Ext.define('PJ.view.myPersonalFiles.ViewSignupStatusListButton', {
    extend: 'AM.default.DefaultList',
    selModel: {
    },
	alias: 'widget.viewManagementListButton',
	
    /*isHavePage: false,
    isPossibleLoad: false,*/
    initComponent: function(){
    	var remarkUrl = Ext.create("Ext.data.ArrayStore", {
    		singleton: true,
    		fields: [{
    			name: 'value',
    			mapping: "dataCode",
    			type: 'string'
    		},
    		{
    			name: 'text',
    			mapping: "dataName",
    			type: 'string'
    		}],
    		proxy: {
    			type: 'ajax',
    			url: this.grid.grid.store.remarkUrl + '?typeCode=1350',
    			reader: Ext.create('Ext.data.reader.Json', {
    				type: 'json',
    				root: 'data',
    				successProperty: 'success'
    			})
    		},
    		autoLoad: true
    	});
        function getStockType2(value) {
            var rowIndex = resultUrl.find("value", "" + value);
            if (rowIndex < 0) return value;
            var record = resultUrl.getAt(rowIndex);
            return record ? record.get('text') : '请选择..';
        };
    	var resultUrl = Ext.create("Ext.data.ArrayStore", {
    		singleton: true,
    		fields: [{
    			name: 'value',
    			mapping: "dataCode",
    			type: 'string'
    		},
    		{
    			name: 'text',
    			mapping: "dataName",
    			type: 'string'
    		}],
    		proxy: {
    			type: 'ajax',
    			url: this.grid.grid.store.remarkUrl + '?typeCode=1355',
    			reader: Ext.create('Ext.data.reader.Json', {
    				type: 'json',
    				root: 'data',
    				successProperty: 'success'
    			})
    		},
    		autoLoad: true
    	});
        function getStockType1(value) {
            var rowIndex = remarkUrl.find("value", "" + value);
            if (rowIndex < 0) return value;
            var record = remarkUrl.getAt(rowIndex);
            return record ? record.get('text') : '请选择..';
        };
    	this.columns = [{
        	header: '学员帐号',
        	dataIndex: 'bu.login_id',
        	width: 75
        },{
        	header: '姓名',
        	dataIndex: 'bu.user_Name',
        	width: 75
        }, {
            header: '性别',
            dataIndex: 'bb.order_num',
            width: 45
        },{
            header: '出生年月',
            dataIndex: 'c.birthday',
            hidden:true, 
            width: 70,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '民族',
            dataIndex: 'bt.order_num',
            hidden:true,  
            width: 45,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },{
            header: '手机号',
            dataIndex: 'c.mobile_Telephone',
            width: 100
        }, {
            header: '单位',
            dataIndex: 'd.CUSTOMFIELD6',
            width: 120,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
        }
        },{
            header: '职级',
            dataIndex: 'bbb.order_num',
            flex:.5,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: '单位及职务',
            dataIndex: 'c.duty_remark',
            flex: .7,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        },{
			header: '学时',
			dataIndex: 'trainingHour',
			width: 50,
//            renderer: function(value, meta, record){
//            	meta.tdAttr = "classUserId='" + record.get("classUserId") + "'";
//                return value;
//            },
			editor: {
				xtype: 'textfield',
				regexText: '请输入正整数',
				maxLength: 8, 
				regex: /^[1-9]*[0-9][0-9]*$/,
//				regex:/^([1-9]\d{0,15}|0)(\.\d{1,2})?$/,
				listeners: {
					focus:function(){
						var value = this.getValue();
						if(value.length>8){
							return;
						}
						var record = this.up("grid").getSelectionModel().getLastSelected();
						this.classUserId=record.get("classUserId");
					},
					blur: function(v){
						var me = this;
						var value = this.getValue();
						if(value.length>8){
							return;
						}
						var url = me.up("grid").store.updateUserHUrl;
						var Id=this.classUserId;
						if(this.regex.test(value)){
							Ext.Ajax.request({
								url: url,
								params: {
									searchClassUserId: Id,
									trainingHour:  value
								},
								success: function(response){
									var data = response.responseText;
									var json = Ext.decode(data, true);
									if (json.success) {
										me.up("grid").store.load();
									}
									else {
										Orchid.alert('消息', json.msg, function(){
											me.up("grid").store.load();
										});
									}
								}
							});
						}else{
							Orchid.alert('消息','请输入正整数');
						}
					}
				}
			}
        },{
			header: '备注',
			dataIndex: 'c.examination_remark',
			flex:.2,
            renderer: getStockType1,
			editor: new Ext.form.ComboBox({
				store: remarkUrl,
                valueField: 'value',
                displayField: 'text',
//                editable: false,//是否编辑
                defaultActive: false,
				maxLength: 1500, 
				listeners: {
					focus:function(){
						var record = this.up("grid").getSelectionModel().getLastSelected();
						this.classUserId=record.get("classUserId");
					},
					blur: function(v){
						var me = this;
						var value = this.getValue();
						if(value.length>1500){
							return;
						}
						var url = me.up("grid").store.updateUserBUrl;
						var Id=this.classUserId;
						Ext.Ajax.request({
							url: url,
							params: {
								searchClassUserId: Id,
								examinationRemark:  value
							},
							success: function(response){
								var data = response.responseText;
								var json = Ext.decode(data, true);
								if (json.success) {
									me.up("grid").store.load();
								}
								else {
									Orchid.alert('消息', json.msg, function(){
										me.up("grid").store.load();
									});
								}
							}
						});
					}
				}
			})
        },{
			header: '考核状态',
			dataIndex: 'c.examination_result',
			width: 70,
            renderer: getStockType2,
			editor: new Ext.form.ComboBox({
				store: resultUrl,
                valueField: 'value',
                displayField: 'text',
                editable: false,//是否编辑
                defaultActive: false,
				maxLength: 1500, 
				listeners: {
					focus:function(){
						var record = this.up("grid").getSelectionModel().getLastSelected();
						this.classUserId=record.get("classUserId");
					},
					blur: function(v){
						var me = this;
						var value = this.getValue();
						if(value.length>1500){
							return;
						}
						var url = me.up("grid").store.updateUserRUrl;
						var Id=this.classUserId;
						Ext.Ajax.request({
							url: url,
							params: {
								searchClassUserId: Id,
								examinationResult:  value
							},
							success: function(response){
								var data = response.responseText;
								var json = Ext.decode(data, true);
								if (json.success) {
									me.up("grid").store.load();
								}
								else {
									Orchid.alert('消息', json.msg, function(){
										me.up("grid").store.load();
									});
								}
							}
						});
					}
				}
			})
        },{
            header: '锁定状态',
            dataIndex: 'lockState',
            sortable: false,
            width:60,
            renderer: function(value, meta, record){
                meta.tdAttr = 'data-qtip="' + value + '"';
                if(value==1){
                	return '<span style="color:red;">' + '已锁定' + '</span>';
                }else{
                	return '<span style="color:green;">' + '未锁定' + '</span>';
                }
            }
        }],
        this.store = Ext.create('PJ.'+prototype+'.myPersonalFiles.ViewSignupStatusListButton');
		this.plugins = [Ext.create('Ext.grid.plugin.CellEditing', {
	         clicksToEdit: 1,
	         listeners: {
	             beforeedit: function(editor, e) {
	            	 if (e.record.get("lockState") == "1") {
	            		 return false;
	        		 }
	                 var textfield = e.column.getEditor();
	                 if (gj_onlineLearning_constant.loadType == "ajax") {
	                     Ext.Ajax.request({
	                         url: tokenUrl,
	                         success: function(response) {
	                             var data = response.responseText;
	                             var token = Ext.decode(data, true);
	                             textfield.token = token;
	                         }
	                     });
	                 }
	             },
	         }
	    })];
        this.loadType = gj_onlineLearning_constant.loadType;
        this.callParent(arguments);
    },
    //导出
    onExportClick: function(){
    	var me = this;
    	var store = me.getStore();
    	var href=store.exportUrl + "?searchClassID=" + me.classId;
    	href = encodeURI(href);
    	var atag = Ext.get("exportResume");
    	atag.set({
    		"href": href
    	});
    	atag.dom.click();
    },
    //锁定考核学时，备注和考核状态
    onLockClick:function(){
    	var me=this;
    	var json;
    	var result;
    	var record = this.getSelectionModel().store.data.items[0];
    	if(record.get('lockState')==1){
    		Ext.Msg.alert('消息', '考核状态已为锁定状态！', function(){
				  var store = me.getStore();
				  store.load();
			  });
    		return;
    	}
    	Ext.Ajax.request({
			  url: me.store.signSituationUrl + '?searchClassID=' + me.classId,
			  success: function(response){
				  json = Ext.decode(response.responseText, true);
				  if(json.state==200){
					  Ext.Msg.alert('消息', json.message, function(){
						  var store = me.getStore();
						  store.load();
				    	  return;
					  });
				  }
				  result=json.message.split(',');
				  Ext.Msg.confirm({
		    		  title:'提示',
		    		  msg:'请认真核对学员学时，锁定后数据将录入档案系统且无法修改，<br/>本班次共报名学员'+result[0]+'人，考核合格'+result[1]+'人，不合格'+result[2]+'人，未参训'+result[3]+'人。',
		    		  icon:Ext.Msg.WARNING,
		    		  buttons:Ext.Msg.OKCANCEL,
		    		  fn:function(id){
		    			  if(id=='ok'){
		    				  Ext.Ajax.request({
		    					  url: me.store.updateExaminationStateUrl + '?searchClassID=' + me.classId+'&lockState=1',
		    					  success: function(response){
		    						  var data = response.responseText;
		    						  var json = Ext.decode(data, true);
		    						  var jsonData = json.message;
		    						  Ext.Msg.alert('消息', jsonData, function(){
		    							  var store = me.getStore();
		    							  store.load();
		    						  });
		    					  }
		    				  });
						  }
					  }
				  });
			  }
		 });
    },
    listeners: {
//    	selectionchange: function(selModel, selected){
//    		this.down('#' + this.oldId + 'Export').setDisabled(selected.length === 0);
//    	},
    	viewready: function(){
    		if(this.clickSource==2){  
    		this.addDocked([{
                dock: 'top',
                xtype: "searchform",
    			columnCount: 2,
    			collapsedFieldSet: false,
                fieldDefaults: {
                    xtype: "textfield",
                    labelAlign: 'right',
                    labelWidth: 80
                },
                items: [{
                    fieldLabel: '学员帐号',
                    name: "searchLoginID",
                    emptyText: "请输入学员帐号",
        			columnWidth: .5,
                }, {
                    fieldLabel: "姓名",
                    name: "searchUserName",
                    emptyText: "请输入姓名",
        			columnWidth: .5,
                }/*{
                    fieldLabel: '单位',
                    name: "searchDepName",
                    emptyText: "请输入单位",
        			columnWidth: .4,
                },*//*,{
					xtype: "combo",
					fieldLabel: '审核状态',
					name: "auditingState",
					store: Ext.create("Ext.data.ArrayStore", {
						fields: ['value', 'text'],
						data: [[1, '审核通过'],[2,'未审核通过'],[3,'未审核']]
					}),
					valueField: 'value',
					displayField: 'text',
					editable: false,
					emptyText: "请选择"
				}*/],
    			onResetClick: function(button){
    				
                    var form = button.up('form');
                    var gridPanel = form.grid;
                    var newParams = {
            				searchClassID: gridPanel.up('window').classId,
            				clickSource:this.up('window').clickSource,
            				auditingState: 1
                    };
                    gridPanel.getStore().proxy.extraParams = {};
                    var params = gridPanel.getStore().proxy.extraParams;
                    Ext.apply(params, newParams);
                    form.getForm().reset();
                },
                grid: this
            },{
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [{
                    text: '导出',
                    iconCls: 'deriveIco',
                    itemId: this.oldId + "Export",
                    disabled: false,
                    scope: this,
                    handler: this.onExportClick
                }/*, "-", {
                    xtype: 'label',
                    itemId: 'signupQuotaOfPeople',
                    html: ''
                }*/]
            }]);
    		} else{
        		this.addDocked([{
                    dock: 'top',
                    xtype: "searchform",
        			columnCount: 2,
        			collapsedFieldSet: false,
                    fieldDefaults: {
                        xtype: "textfield",
                        labelAlign: 'right',
                        labelWidth: 80
                    },
                    items: [{
                        fieldLabel: '学员帐号',
                        name: "searchLoginID",
                        emptyText: "请输入学员帐号",
            			columnWidth: .25,
                    }, {
                        fieldLabel: "姓名",
                        name: "searchUserName",
                        emptyText: "请输入姓名",
            			columnWidth: .25,
                    },{
                        fieldLabel: '单位',
                        name: "searchDepName",
                        emptyText: "请输入单位",
            			columnWidth: .25,
                    },{
                    	xtype:"combo",
                        fieldLabel: "考核状态",
                        name: "searchExaminationResultType",
                        emptyText: "请选择",
            			columnWidth: .25,
                    	store: Ext.create("Ext.data.ArrayStore", {
                    		singleton: true,
                    		fields: [{
                    			name: 'value',
                    			mapping: "dataCode",
                    			type: 'string'
                    		}, {
                    			name: 'text',
                    			mapping: "dataName",
                    			type: 'string'
                    		}],
                    		proxy: {
                    			type: gj_onlineLearning_constant.loadType,
                    			url: this.store.baseDateTypeUrl + '?typeCode=1355',
                    			reader: Ext.create('Ext.data.reader.Json', {
                    				type: 'json',
                    				root: 'data',
                    				successProperty: 'success'
                    			})
                    		},
                    		autoLoad: true
                    	}),
            			valueField: 'value',
            			displayField: 'text',
            			editable: false,
            			defaultActive: false
                    }/*,{
    					xtype: "combo",
    					fieldLabel: '审核状态',
    					name: "auditingState",
    					store: Ext.create("Ext.data.ArrayStore", {
    						fields: ['value', 'text'],
    						data: [[1, '审核通过'],[2,'未审核通过'],[3,'未审核']]
    					}),
    					valueField: 'value',
    					displayField: 'text',
    					editable: false,
    					emptyText: "请选择"
    				}*/],
        			onResetClick: function(button){
        				
                        var form = button.up('form');
                        var gridPanel = form.grid;
                        var newParams = {
                				searchClassID: gridPanel.up('window').classId,
                				clickSource:this.up('window').clickSource,
                				auditingState: 1
                        };
                        gridPanel.getStore().proxy.extraParams = {};
                        var params = gridPanel.getStore().proxy.extraParams;
                        Ext.apply(params, newParams);
                        form.getForm().reset();
                    },
                    grid: this
                },{
                    xtype: 'toolbar',
                    dock: 'top',
                    displayInfo: true,
                    items: [{
                        text: '锁定',
                        iconCls: 'examinationLock',
                        itemId: this.oldId + "lock",
                        disabled: false,
                        scope: this,
                        handler: this.onLockClick
                    },{
                        text: '导出',
                        iconCls: 'deriveIco',
                        itemId: this.oldId + "Export",
                        disabled: false,
                        scope: this,
                        handler: this.onExportClick
                    }/*, "-", {
                        xtype: 'label',
                        itemId: 'signupQuotaOfPeople',
                        html: ''
                    }*/]
                }]);
    		}
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    		var params = this.store.proxy.extraParams;
    		var newParams = {
    				searchClassID: this.up('window').classId,
    				clickSource:this.up('window').clickSource,
    		};
    		Ext.apply(params, newParams);
    		this.store.isLoad = true;
    		this.store.load();
    		//this.quotaOfPeople();//单位授人数统计
    	}
    }
});
